package security.Example;

import java.net.InetAddress;
import java.net.UnknownHostException;

import security.Authentication.UserPass.UserPassHandler;
import security.Authentication.UserPass.UserPassRequest;
import security.Authorization.UserRole.UserRole;
import security.IntrusionDetection.Base.Email;
import security.IntrusionDetection.IPInstrusion.IPIntrusion;
import security.LoginSystem.Base.LoginSystem;

/**
 * Example application of the IP intrusion QAS
 */
public class ExampleIPIntrusion {

	/**
	 * Run the example application
	 * @param args no arguments is needed
	 * @throws UnknownHostException if the local host was not found
	 */
	public static void main(String[] args) throws UnknownHostException {
		LoginSystem loginSystem = new LoginSystem();
		InetAddress localHost = InetAddress.getLocalHost();
		InetAddress remoteHost = InetAddress.getByName("192.168.1.112");
		UserPassRequest localRequest =
				new UserPassRequest(localHost, "QwertyUser", "Qwerty1.");
		UserPassRequest remoteRequest =
				new UserPassRequest(remoteHost, "QwertyUser", "Qwerty1.");

		// Create IPIntrusion that is an implementation of the abstract class
		// IntrusionDetection to handle the intrusion detection.
		Email email = new Email();
		IPIntrusion ipIntrusion = new IPIntrusion(email);
		
		// Add the authentication and authorization to the system
		loginSystem.setAuthorization(new UserRole());

		// The intrusion detection needs to be added before the authentication
		// if the authentication should be protected by the intrusion detection.
		// This is because of the use of the pattern Chain of responsibility and
		// a proxy that makes the intrusion detection act as an authentication 
		// handler before the real authentication handler will be called in the 
		// chain.
		loginSystem.addIntrusionDetection(ipIntrusion);
		loginSystem.addAuthentication(new UserPassHandler());

		// Test that the IP intrusion works
		loginSystem.login(localRequest);
		loginSystem.login(remoteRequest);
	}
}
